package com.mas.user;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.mas.common.Consts;
import com.mas.resource.ResourceInfo;

/**
 * 
 * @author Dudi & Efi
 * 
 */
class UserResourceAgentGui extends JFrame {

	private JTextField txtAmount;
	private JTextField txtStartHour;
	private JTextField txtEndHour;
	private JTextField txtPrivateValue;

	private JTextField txtPercent;
	private JTextField txtPricePerKwt;
	private JTextField txtPriceDisk;
	private JTextField txtPriceNet;
	
	protected UserResourceAgent agent;

	UserResourceAgentGui(final UserResourceAgent agent) {
		super(agent.getLocalName());
		this.agent = agent;

		JPanel p = new JPanel();
		p.setLayout(new GridLayout(7, 2));
		//headers
		p.add(new JLabel(""));
		p.add(new JLabel("Client Parameters"));
		
		p.add(new JLabel("Amount(kWT):"));
		txtAmount = new JTextField("0", 15);
		p.add(txtAmount);
		p.add(new JLabel("Acceptable Percent:"));
		txtPercent = new JTextField("0", 15);
		p.add(txtPercent);
		
		p.add(new JLabel("Start Hour (24 hours):"));
		txtStartHour = new JTextField("0", 15);
		p.add(txtStartHour);
		
		p.add(new JLabel("End Hour (24 hours):"));
		txtEndHour = new JTextField("0", 15);
		p.add(txtEndHour);
		
		p.add(new JLabel("Private Value:"));
		txtPrivateValue = new JTextField("0", 15);
		p.add(txtPrivateValue);
		
		p.add(new JLabel("Price per kWT:"));
		txtPricePerKwt = new JTextField("0", 15);
		p.add(txtPricePerKwt);
		getContentPane().add(p, BorderLayout.CENTER);

		JButton startNegotiateBtn = new JButton("Start Negotiate");
		startNegotiateBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					double amount = Double.parseDouble(txtAmount.getText().trim());
					if (amount < 0)
						throw new Exception("Invalid values. Amount must be positive.");

					int startTime = Integer.parseInt(txtStartHour.getText().trim());
					if (startTime < 0 || startTime > 23)
						throw new Exception("Invalid values. Start time must be between 0 and 23.");

					int endTime = Integer.parseInt(txtEndHour.getText().trim());
					if (endTime < 0 || endTime > 23)
						throw new Exception("Invalid values. End time must be between 0 and 23.");

					double privateValue = Double.parseDouble(txtPrivateValue.getText().trim());
					if (privateValue < 0)
						throw new Exception("Invalid values. Private value be positive.");
					
					double percentAllowed = Double.parseDouble(txtPercent.getText().trim());
					if (percentAllowed < 0 || percentAllowed > 100) {
						throw new Exception("Invalid values. Acceptable percent must be between 0 and 100.");
					}
					
					double pricePerKwt = Double.parseDouble(txtPricePerKwt.getText().trim());
					if (pricePerKwt < 0) {
						throw new Exception("Invalid values. Price per kWT must be positive.");
					}
					List<ResourceInfo> resourceList = new ArrayList<ResourceInfo>();

					
					if (amount > 0) {
						ResourceInfo resourceInfo = new ResourceInfo();
						resourceInfo.setResourceName(Consts.RESOURCE_AMOUNT_TO_BUY);
						resourceInfo.setRequiredUsage(amount);
						resourceInfo.setPrice(pricePerKwt);
						resourceList.add(resourceInfo);
					}
					if (startTime >= 0) {
						ResourceInfo resourceInfo = new ResourceInfo();
						resourceInfo.setResourceName(Consts.RESOURCE_START_TIME);
						resourceInfo.setRequiredUsage(startTime);
						resourceInfo.setPrice(startTime);
						resourceList.add(resourceInfo);
					}
					if (endTime >= 0) {
						ResourceInfo resourceInfo = new ResourceInfo();
						resourceInfo.setResourceName(Consts.RESOURCE_END_TIME);
						resourceInfo.setRequiredUsage(endTime);
						resourceInfo.setPrice(endTime);
						resourceList.add(resourceInfo);
					}

					agent.startNegotiate(resourceList);

				} catch (Exception e) {
					JOptionPane.showMessageDialog(UserResourceAgentGui.this,
							 e.getMessage(), "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		p = new JPanel();
		p.setLayout(new GridLayout(1, 2));
		p.add(startNegotiateBtn);
		JButton doneBtn= new JButton("Done");
		doneBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				agent.doDelete();
				dispose();
			}});
		p.add(doneBtn);
		getContentPane().add(p, BorderLayout.SOUTH);

		// Make the agent terminate when the user closes
		// the GUI using the button on the upper right corner
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				agent.doDelete();
			}
		});

		setResizable(false);
	}

	public void show() {
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int) screenSize.getWidth() / 2;
		int centerY = (int) screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.show();
	}
}
